package com.sprinboot_vue.blog_springboot.controller;

import com.sprinboot_vue.blog_springboot.entity.Blog_friend;
import com.sprinboot_vue.blog_springboot.repository.Blog_friendRepository;
import com.sprinboot_vue.blog_springboot.result.Result;
import com.sprinboot_vue.blog_springboot.result.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Thomas
 * @version 1.0
 * @date 2021/11/24 13:25
 */
@RestController
@RequestMapping("/blog_friend")
@Api(tags = "友链管理")
public class Blog_friendHandler {
    @Autowired
    private Blog_friendRepository blog_friendRepository;

    @PostMapping("/saveFriend")
    @ApiOperation("保存友链的相关信息")
    public Result Blog_friendSave(@RequestBody Blog_friend blog_friend){
        blog_friend.setTime_(BlogGetDate(new Date()));
        try{
            blog_friendRepository.save(blog_friend);
            return Result.success().codeAndmessage(ResultInfo.SUCCESS);
        }catch (Exception e){
            return Result.error().codeAndmessage(ResultInfo.ERROR);
        }
    }
    //获得当前的时间  2021.12.23 12:23:59
    private String BlogGetDate(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(date);
    }

    @GetMapping("/findAllFriend")
    @ApiOperation("返回所有的友链信息")
    public Result findAllFriend(){
        List<Blog_friend> all = blog_friendRepository.findAll();
        return Result.success().codeAndmessage(ResultInfo.SUCCESS).date("data",all);
    }
}
